#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LinkTree.h"

#define true 1
#define false 0
struct student
{
    int id;;
    char* name;
    int score;
};

struct student* CreateStudent(int id, const char* name, int score)
{
    struct student* stu = (struct student*)malloc (sizeof(struct student));
    stu->id = id;
    stu->name = (char*)name;
    stu->score = score;
}

void FreeStudent(struct student* stu)
{
    if(stu  == NULL)
        return;
    free(stu);
}

void print(ElementType data)
{
    if(data.type == structPoint)
    {
        struct student* stu = (struct student*)data.value;
        if(stu != NULL)
        {
            printf("id : %d , name : %s, score : %d\n", stu->id, stu->name, stu->score);
        }
    }
    else if(data.type == charPoint)
    {
        char* temp = (char*)data.value;
        printf("%s\n", temp);
    }

}

int IsEqual(ElementType data1, ElementType data2)
{
    if(data1.type == structPoint && data2.type == structPoint)
    {
        struct student* stu1 = (struct student*)data1.value;
        struct student* stu2 = (struct student*)data2.value;
        if(stu1->id == stu2->id)
            return true;
        return false; 
    }
    else if(data1.type == charPoint && data2.type == charPoint)
    {
        char* temp1 = (char*)data1.value;
        char* temp2 = (char*)data2.value;
        if(strcmp(temp1, temp2) == 0)
            return true;
        return false; 
    }
}

int main()
{
    LTree tree;
    InitLTree(&tree);
    
    struct UniversalType TypeValue;
    TypeValue.value = "243class";
    TypeValue.type = charPoint;
    LTNode* node1 = CreateTreeNode(TypeValue);

    TypeValue.value = CreateStudent(1, "zhangsan", 100);
    TypeValue.type = structPoint;
    LTNode* node2 = CreateTreeNode(TypeValue);

    TypeValue.value = CreateStudent(2, "lisi", 90);
    TypeValue.type = structPoint;
    LTNode* node3 = CreateTreeNode(TypeValue);
    //LTNode* node4 = CreateTreeNode("shaojun");
    ConnectBranch(node1, node2);
    ConnectBranch(node1, node3);
    // ConnectBranch(node1, node4);

    // LTNode* node5 = CreateTreeNode("243class");
    // LTNode* node6 = CreateTreeNode("liangtianchi");
    // LTNode* node7 = CreateTreeNode("wanghan");
    // LTNode* node8 = CreateTreeNode("shaojun");
    // ConnectBranch(node5, node6);
    // ConnectBranch(node5, node7);
    // ConnectBranch(node5, node8);

    ConnectBranch(tree.root, node1);
    // ConnectBranch(tree.root, node5);

    TravelTree(&tree,print);

    // printf("height : %d\n", GetTreeHeight(&tree));

    // LTNode* classNode = FindTreeNode(&tree, "247class");
    // if(classNode != NULL)
    // {
    //     LTNode* newNode = CreateTreeNode("zhangqiantao");
    //     ConnectBranch(classNode, newNode);
    // }
    // //TravelTree(&tree);
    TypeValue.type = charPoint;
    TypeValue.value = "243class";
    LTNode* findNode = FindTreeNode(&tree, TypeValue, IsEqual);
    if(findNode != NULL)
    {
        char* className = "247class";
        findNode->data.value = className;
    }
    TravelTree(&tree,print);

    TypeValue.type = structPoint;
    TypeValue.value = CreateStudent(1, "", 100);
    LTNode* findNode1 = FindTreeNode(&tree, TypeValue, IsEqual);
    if(findNode != NULL)
    {
        struct student* stu = (struct student*)findNode1->data.value;
        stu->score = 60;
    }
    free(TypeValue.value);
    TravelTree(&tree,print);





    
    return 0;
}